/*
Run this script on:

        mssql-ct.db.jwan.it,1433.Caronte2013    -  This database will be modified

to synchronize it with:

        kalgan01.cs.jwan.it,1443.Jambo2014_sviluppo

You are recommended to back up your database before running this script

Script created by SQL Compare version 9.0.0 from Red Gate Software Ltd at 10/04/2014 14:52:32

*/
SET NUMERIC_ROUNDABORT OFF
GO
SET ANSI_PADDING, ANSI_WARNINGS, CONCAT_NULL_YIELDS_NULL, ARITHABORT, QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
IF EXISTS (SELECT * FROM tempdb..sysobjects WHERE id=OBJECT_ID('tempdb..#tmpErrors')) DROP TABLE #tmpErrors
GO
CREATE TABLE #tmpErrors (Error int)
GO
SET XACT_ABORT ON
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO
BEGIN TRANSACTION
GO
PRINT N'Dropping foreign keys from [dbo].[configlinee]'
GO
ALTER TABLE [dbo].[configlinee] DROP
CONSTRAINT [FK_configlinee_linee]
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Dropping foreign keys from [dbo].[configtasse]'
GO
ALTER TABLE [dbo].[configtasse] DROP
CONSTRAINT [FK_configtasse_tasse],
CONSTRAINT [FK_configtasse_valute],
CONSTRAINT [FK_configtasse_itiner],
CONSTRAINT [FK_configtasse_catego],
CONSTRAINT [FK_configtasse_iva]
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Dropping constraints from [dbo].[configtasse]'
GO
ALTER TABLE [dbo].[configtasse] DROP CONSTRAINT [PK_configtasse]
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Dropping constraints from [dbo].[configtasse]'
GO
ALTER TABLE [dbo].[configtasse] DROP CONSTRAINT [DF_configtasse_CODSCO]
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Dropping constraints from [dbo].[configtasse]'
GO
ALTER TABLE [dbo].[configtasse] DROP CONSTRAINT [DF_configtasse_CODIVA]
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Dropping constraints from [dbo].[configtasse]'
GO
ALTER TABLE [dbo].[configtasse] DROP CONSTRAINT [DF_configtasse_CODLIST]
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Dropping constraints from [dbo].[configtasse]'
GO
ALTER TABLE [dbo].[configtasse] DROP CONSTRAINT [DF_configtasse_CODTAR]
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Dropping constraints from [dbo].[configtasse]'
GO
ALTER TABLE [dbo].[configtasse] DROP CONSTRAINT [DF_configtasse_ADU]
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Dropping constraints from [dbo].[configtasse]'
GO
ALTER TABLE [dbo].[configtasse] DROP CONSTRAINT [DF_configtasse_CHD]
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Dropping constraints from [dbo].[configtasse]'
GO
ALTER TABLE [dbo].[configtasse] DROP CONSTRAINT [DF_configtasse_INF]
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Dropping constraints from [dbo].[configtasse]'
GO
ALTER TABLE [dbo].[configtasse] DROP CONSTRAINT [DF_configtasse_MTL]
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Rebuilding [dbo].[configtasse]'
GO
CREATE TABLE [dbo].[tmp_rg_xx_configtasse]
(
[CODTASSA] [varchar] (4) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[CODVAL] [char] (3) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[CODITINERA] [char] (4) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[CATKEY] [int] NOT NULL,
[TASSA] [decimal] (18, 2) NOT NULL,
[VALIDO_DAL] [datetime] NOT NULL,
[VALIDO_AL] [datetime] NOT NULL,
[CODSCO] [char] (2) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL DEFAULT ('*'),
[CODIVA] [char] (3) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL DEFAULT ((0)),
[CODLIST] [char] (3) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL DEFAULT ('*'),
[CODTAR] [char] (2) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL DEFAULT ('*'),
[ADU] [bit] NOT NULL DEFAULT ((1)),
[CHD] [bit] NOT NULL DEFAULT ((1)),
[INF] [bit] NOT NULL DEFAULT ((1)),
[MTL] [bit] NOT NULL DEFAULT ((0)),
[CODLOCALE] [char] (4) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL DEFAULT('*')
)
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
INSERT INTO [dbo].[tmp_rg_xx_configtasse]([CODTASSA], [CODVAL], [CODITINERA], [CATKEY], [TASSA], [VALIDO_DAL], [VALIDO_AL], [CODSCO], [CODIVA], [CODLIST], [CODTAR], [ADU], [CHD], [INF], [MTL]) SELECT [CODTASSA], [CODVAL], [CODITINERA], [CATKEY], [TASSA], [VALIDO_DAL], [VALIDO_AL], [CODSCO], [CODIVA], [CODLIST], [CODTAR], [ADU], [CHD], [INF], [MTL] FROM [dbo].[configtasse]
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
DROP TABLE [dbo].[configtasse]
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
EXEC sp_rename N'[dbo].[tmp_rg_xx_configtasse]', N'configtasse'
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Creating primary key [PK_configtasse] on [dbo].[configtasse]'
GO
ALTER TABLE [dbo].[configtasse] ADD CONSTRAINT [PK_configtasse] PRIMARY KEY CLUSTERED  ([CODTASSA], [CODVAL], [CODITINERA], [CATKEY], [VALIDO_DAL], [VALIDO_AL], [CODSCO], [CODLIST], [CODTAR], [CODLOCALE])
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Adding foreign keys to [dbo].[configlinee]'
GO
ALTER TABLE [dbo].[configlinee] ADD
CONSTRAINT [FK_configlinee_linee] FOREIGN KEY ([CODLIN]) REFERENCES [dbo].[linee] ([CODLIN])
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Adding foreign keys to [dbo].[configtasse]'
GO
ALTER TABLE [dbo].[configtasse] ADD
CONSTRAINT [FK_configtasse_tasse] FOREIGN KEY ([CODTASSA]) REFERENCES [dbo].[tasse_diritti] ([CODTASSA]),
CONSTRAINT [FK_configtasse_valute] FOREIGN KEY ([CODVAL]) REFERENCES [dbo].[valute] ([CODVAL]),
CONSTRAINT [FK_configtasse_itiner] FOREIGN KEY ([CODITINERA]) REFERENCES [dbo].[itiner] ([CODITINERA]),
CONSTRAINT [FK_configtasse_catego] FOREIGN KEY ([CATKEY]) REFERENCES [dbo].[catego] ([CATKEY]),
CONSTRAINT [FK_configtasse_iva] FOREIGN KEY ([CODIVA]) REFERENCES [dbo].[iva] ([CODIVA])
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Altering permissions on [dbo].[configtasse]'
GO
GRANT SELECT ON  [dbo].[configtasse] TO [JamboClient]
GRANT INSERT ON  [dbo].[configtasse] TO [JamboClient]
GRANT DELETE ON  [dbo].[configtasse] TO [JamboClient]
GRANT UPDATE ON  [dbo].[configtasse] TO [JamboClient]
GRANT SELECT ON  [dbo].[configtasse] TO [JamboGest]
GRANT INSERT ON  [dbo].[configtasse] TO [JamboGest]
GRANT DELETE ON  [dbo].[configtasse] TO [JamboGest]
GRANT UPDATE ON  [dbo].[configtasse] TO [JamboGest]
GO
IF EXISTS (SELECT * FROM #tmpErrors) ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT>0 BEGIN
PRINT 'The database update succeeded'
COMMIT TRANSACTION
END
ELSE PRINT 'The database update failed'
GO
DROP TABLE #tmpErrors
GO
